// pages/im/com/chat-item/chat-item.js
const computedBehavior = require('miniprogram-computed')

Component({
  behaviors: [computedBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    model: {
      type: Object,
      default: {},
    },
    //自己的消息
    isMine: {
      type: Boolean,
      default: false,
    },
    targetAvatar: {
      type: String,
      default: "",
    },
    mineAvatar: {
      type: String,
      default: "",
    },
    audioUuid: {
      type: String,
      default: ""
    },
  },

  options: {
    styleIsolation: "apply-shared",
    addGlobalClass: true,
  },

  /**
   * 组件的初始数据
   */
  data: {
    _model: {},
    avatar: "",
    isPeerRead: false, //此处单拎出来才能更新（原因未知）
  },

  watch: {
    'model.isPeerRead': function(a, b) {
      if (a) {
        // console.log('更新isPeerRead');
        this.setData({ isPeerRead: a })
      }
    },
  },

  lifetimes: {
    attached: function () {
      let model = this.data.model;
      let _model = { ...model };
      let avatar = this.data.isMine ? this.data.mineAvatar : this.data.targetAvatar;

      this.setData({
        _model,
        avatar: avatar || "/images/default_head.png",
      });
    },

    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handlePreview() {
      wx.previewImage({
        urls: [this.data._model.payload.imageInfoArray[1].url],
      })
    },

    handleLoadImage() {
      this.triggerEvent('onimageload')
    },
    
    handlePlay() {
      this.triggerEvent('onaudioplay', {url: this.data._model.payload.url, uuid: this.data._model.payload.uuid})
    },
  },
});
